package com.et.u0

import android.content.Intent
import android.os.Bundle
import android.util.Log
import android.view.LayoutInflater
import android.widget.Toast
import androidx.appcompat.app.AppCompatActivity
import androidx.lifecycle.ViewModelProvider
import com.et.core.init
import com.et.core.out.out.OutBroadcast
import com.et.core.out.out.OutHelper
import com.et.core.release
import com.et.logger.ELog
import com.et.logger.TAGS
import com.et.u0.databinding.ActivityMainBinding
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch
import java.util.*

class MainActivity : AppCompatActivity() {

    private val outHelperCall = {
        OutHelper.binderService(applicationContext)
    }

    private lateinit var binding: ActivityMainBinding
    private lateinit var mainModel: MainModel
    private var sendLaunchBroadcast: Boolean = false

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        ELog.d(TAGS.vending, "vending onCreate ${hashCode()} ${intent?.getStringExtra("launch")}")
        ActivityMainBinding.inflate(LayoutInflater.from(this)).also {
            binding = it
            setContentView(it.root)
        }
        OutHelper.addDeathCallBack(outHelperCall)
        initData()
        initEvent()

        CoroutineScope(Dispatchers.IO).launch {
            ELog.d(TAGS.vending, "vending initialize")
            init(applicationContext)
        }

//        Timer().schedule(object : TimerTask() {
//            override fun run() {
//                Log.d(TAGS.vending, "testInsertsGoods")
//                val list = mutableListOf<ChannelEntity>()
//                for (i in 1..48) {
//                    list.add(ChannelEntity.create(11, i).apply {
//                        status = ENABLED
//                        priceCent = i
//                    })
//                }
//                GoodsHelper.getInstance().adds(list)
//            }
//        }, 5_000)
    }

    private fun initData() {
        mainModel =
            ViewModelProvider(this, ViewModelProvider.AndroidViewModelFactory(application)).get(
                MainModel::class.java
            )
    }

    private fun initEvent() {

        OutBroadcast.addCallBack {
            Toast.makeText(applicationContext, "OutBroadcast", Toast.LENGTH_SHORT).show()
        }

        binding.testOutGoods.setOnClickListener {
            Log.d(TAGS.vending, "testOutGoods")
            val serial = (System.currentTimeMillis() % 1000000).toInt()
            Log.d(TAGS.vending, "$serial")
            val b = OutHelper.outGoods(9, 1, 9999, 42, serial)
            Toast.makeText(this, "Out:$b", Toast.LENGTH_SHORT).show()
        }



        mainModel.initGoodsHelper(this)


    }

    override fun onResume() {
        super.onResume()
        ELog.d(TAGS.vending, "vending onResume ${hashCode()} ${intent?.getStringExtra("launch")}")
        if (sendLaunchBroadcast) {
            CoroutineScope(Dispatchers.IO).launch {
                intent?.let {
                    it.getStringExtra("launch") ?: return@let
                    Intent().apply {
                        action = "com.et.action.launch"
                        ELog.d(TAGS.vending, "vending:send launch broadcast")
                        sendBroadcast(intent)
                    }
                }
            }
        }
        sendLaunchBroadcast = true
    }

    override fun onDestroy() {
        super.onDestroy()
        ELog.d(TAGS.vending, "vending onDestroy ${hashCode()}")

        CoroutineScope(Dispatchers.IO).launch {
            release(applicationContext)
        }
    }

}